package com.bh.weapp.controller.sysuser.led;

import com.bh.common.enums.OrderStatus;
import com.bh.common.core.controller.BaseController;
import com.bh.common.core.domain.AjaxResult;
import com.bh.order.domain.JcLed;
import com.bh.order.service.IJcLedService;
import com.bh.order.vo.JcLedLoadingDetailVo;
import com.bh.order.vo.JcLedPreSendDetailVo;
import com.bh.order.vo.JcLedSignReceivingVo;
import com.bh.order.vo.JcLedUnLoadingDetailVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分段订单Controller
 *
 * @author slx
 * @date 2023-05-30
 */
@Api(tags = {"SYS分段订单Api"})
@RestController
@RequestMapping("/api/sysuser/led")
public class JcLedSysApiController extends BaseController {

    @Autowired
    private IJcLedService jcLedService;


    /**
     * 查询分段订单列表
     */
    @ApiOperation(value = "查询已经装车订单及数量", notes = "查询已经装车订单及数量")
    @PostMapping("/getLoadList/{shipmentId}")
    public AjaxResult getLoadList(@PathVariable("shipmentId") Long shipmentId) {
        List<JcLedLoadingDetailVo> list = jcLedService.getLoadList(shipmentId);
        return AjaxResult.success(list);
    }


    /**
     * 查询分段订单列表
     */
    @ApiOperation(value = "查询应该但未保存卸车订单及数量", notes = "查询应该卸车订单及数量")
    @PostMapping("/getUnLoadList/{shipmentId}")
    public AjaxResult getUnLoadList(@PathVariable("shipmentId") Long shipmentId) {
        List<JcLedUnLoadingDetailVo> list = jcLedService.getUnLoadList(shipmentId);
        return AjaxResult.success(list);
    }


    /**
     * 查询分段订单列表
     */
    @ApiOperation(value = "查询派送中订单及数量", notes = "查询派送中订单及数量")
    @GetMapping("/getPreSendList/{sendId}")
    public AjaxResult getPreSendList(@PathVariable("sendId") Long sendId) {
        List<JcLedPreSendDetailVo> list = jcLedService.getPreSendList(sendId);
        return AjaxResult.success(list);
    }

    /**
     * 修改分段订单
     */
    @ApiOperation(value = "查询分段订单详情", notes = "查询分段订单详情")
    @PreAuthorize("@ss.hasPermi('api:order:led:info')")
    @GetMapping("/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        JcLed jcLed = jcLedService.selectJcLedById(id);
        return AjaxResult.success(jcLed);
    }


    /**
     * 派送员分段订单签收
     */
    @Transactional(rollbackFor = Exception.class)
    @PreAuthorize("@ss.hasPermi('api:order:led:sign')")
    @ApiOperation("派送员分段订单签收")
    @PostMapping("/signReceiving")
    public AjaxResult signReceiving(@Validated @RequestBody JcLedSignReceivingVo signVo) {
        JcLed jcLed = jcLedService.selectJcLedById(signVo.getLedId());
        if (jcLed.getStatus() != OrderStatus.TRANSPORT_FINISHED.getValue() && jcLed.getStatus() != OrderStatus.READY_SEND.getValue()) {
            return error("订单未到达，禁止签收！");
        }
        signVo.setUserId(getUserId().toString());
        return toAjax(jcLedService.signReceiving(signVo));
    }
}